package model

import (
	"fmt"
	"gowork/orderProcess/utils"
	"strconv"
	"strings"
	"time"
)

type PutOrder struct {
	OrderType int
	CustomerId int64
	CustomerName string
	NextSinglePersonId int64
	NextSinglePersonName string
	MealList string
	TotalPrice int64
	Region string
}

type Order struct {
	Id int64 `xorm:"pk autoincr"`
	Type int
	State int
	CustomerId int64
	CustomerName string
	NextSinglePersonId int64
	NextSinglePersonName string
	MealList string
	TotalPrice int64
	Region string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Meal struct{
	Id int64 `xorm:"pk autoincr"`
	Type int
	MealName string
	Price int64
	Number int64
	Entity bool
 	CreatedAt time.Time
	UpdatedAt time.Time
}

type AuditType struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type DepartMentAudit struct {
	Id int64 `xorm:"pk autoincr"`
	DepartMentId int64
	AuditTypeId int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Adviser struct {
	Id int64 `xorm:"pk autoincr"`
	DepartMentId int64
	RoleId int64
	Name string
	CreatedAt time.Time
	UpdatedAt time.Time
}

//type LargeOrderAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}
//
//type GeneralAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}
//
//type BusinessAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}
//
//type SupervisorAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}
//
//type DirectorAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}

type OrderActivation struct {
	Id int64 `xorm:"pk autoincr"`
	OrderId int64
	Activating int64
	State int
	Result int
	CreatedAt time.Time
	UpdatedAt time.Time
}

//type EmotionalCounselingAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}
//
//type EmotionalAnalysis struct {
//	Id int64 `xorm:"pk autoincr"`
//	OrderId int64
//	Analyst int64
//	State int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}

type ExpressChange struct {
	Id int64 `xorm:"pk autoincr"`
	OrderId int64
	AdviserId int64
	Express int
	State int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ExpressAudit struct {
	Id int64 `xorm:"pk autoincr"`
	ExpressChangeId int64
	OrderId int64
	Reviewer int64
	ReviewerName string
	State int
	ExpressChangeStatus int
	CreatedAt time.Time
	UpdatedAt time.Time
}

//type SupervisorExpressAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	ExpressChangeId int64
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}
//
//type PinTubeExpressAudit struct {
//	Id int64 `xorm:"pk autoincr"`
//	ExpressChangeId int64
//	OrderId int64
//	Reviewer int64
//	State int
//	Result int
//	CreatedAt time.Time
//	UpdatedAt time.Time
//}

func (expressChange *ExpressChange)OrderAddressChange(oId int64, aId int64, e int)(map[string]interface{}, error){

	w := new(utils.Worker)

	var ec = ExpressChange{
		Id:      w.GetId(),
		OrderId: oId,
		AdviserId: aId,
		Express: e,
		State:   1,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
	x.Insert(&ec)

	var sea = ExpressAudit{
		Id:              w.GetId(),
		ExpressChangeId: ec.Id,
		OrderId:   oId,
		State:     1,
		ExpressChangeStatus: 1,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
	x.Insert(&sea)

	return map[string]interface{}{"code":200, "message":"成功"}, nil
}

func (expressChange *ExpressChange)QueryExpressAudit(adviserId int64)([]*ExpressAudit, error) {

	var expressAuditList []*ExpressAudit

	var adviser Adviser
	x.Id(adviserId).Get(&adviser)

	if adviser.RoleId == 305814115698343941{
		x.Where("express_change_status = ?", 1).Find(&expressAuditList)
	}else if adviser.RoleId == 305814115698343944{
		x.Where("express_change_status = ?", 2).Find(&expressAuditList)
	}

	return expressAuditList, nil
}

func (expressChange *ExpressChange)AddressConfirmation(aId int64, eaId int64)(map[string]interface{}, error){

	w := new(utils.Worker)

	var ea ExpressAudit
	x.Id(eaId).Get(&ea)

	var a Adviser
	x.Id(aId).Get(&a)

	var ec ExpressChange

	if ea.ExpressChangeStatus == 1 {
		var ptea = ExpressAudit{
			Id:                  w.GetId(),
			ExpressChangeId:     ea.ExpressChangeId,
			OrderId:             ea.OrderId,
			State:               1,
			ExpressChangeStatus: 2,
			CreatedAt:           time.Now(),
			UpdatedAt:           time.Now(),
		}
		x.Insert(&ptea)

		ea.Reviewer = aId
		ea.ReviewerName = a.Name
		ea.State = 2
		x.Id(ea.Id).Update(&ea)

		ec.State = 2
		x.Id(ea.ExpressChangeId).Update(&ec)

	}else if ea.ExpressChangeStatus == 2{
		ea.Reviewer = aId
		ea.ReviewerName = a.Name
		ea.State = 2
		x.Id(ea.Id).Update(&ea)

		ec.State = 3
		x.Id(ea.ExpressChangeId).Update(&ec)
	}

	return map[string]interface{}{"code":200, "message":"审核通过"}, nil
}

//func SupervisorAddressConfirmation(aId int64, seaId int64)(map[string]interface{}, error){
//
//	var sea SupervisorExpressAudit
//	x.Id(seaId).Get(&sea)
//
//	var ec ExpressChange
//
//	var o Order
//
//	var ptea = PinTubeExpressAudit{
//		ExpressChangeId: sea.ExpressChangeId,
//		OrderId:   sea.OrderId,
//		State:     1,
//		CreatedAt: time.Now(),
//		UpdatedAt: time.Now(),
//	}
//	x.Insert(&ptea)
//
//	sea.Reviewer = aId
//	sea.State = 1
//	x.Id(sea.Id).Update(&sea)
//
//	ec.State = 1
//	x.Id(sea.ExpressChangeId).Update(&ec)
//
//	o.State = -2
//	x.Id(sea.OrderId).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func PinTubeAddressConfirmation(aId int64, pteaId int64)(map[string]interface{}, error){
//
//	var ptea PinTubeExpressAudit
//	x.Id(pteaId).Get(&ptea)
//
//	var ec ExpressChange
//
//	var o Order
//
//	ptea.Reviewer = aId
//	ptea.State = 1
//	x.Id(ptea.Id).Update(&ptea)
//
//	ec.State = 2
//	x.Id(ptea.ExpressChangeId).Update(&ec)
//
//	o.State = 1
//	x.Id(ptea.OrderId).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}

func (order *Order)PlaceOrder(po *PutOrder)(map[string]interface{}, error){

	w := new(utils.Worker)

	var o = Order{
		Id:                   w.GetId(),
		Type:                 po.OrderType,
		State:                1,
		CustomerId:           po.CustomerId,
		CustomerName:         po.CustomerName,
		NextSinglePersonId:   po.NextSinglePersonId,
		NextSinglePersonName: po.NextSinglePersonName,
		MealList:             po.MealList,
		TotalPrice:           po.TotalPrice,
		Region:               po.Region,
		CreatedAt:            time.Now(),
		UpdatedAt:            time.Now(),
	}
	x.Insert(&o)

	if po.OrderType == 5{
		if po.TotalPrice < 30000{
			var sa = Audit{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				OrderStatus: -1,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&sa)

			o.State = -1
			x.Id(o.Id).Update(&o)
		}
	//}else if po.OrderType == 1 || po.OrderType == 7 || po.OrderType == 11 {
	//	var entityProductList []*Meal
	//	x.Where("entity = ?", true).Find(&entityProductList)
	//	for _, m := range strings.Split(po.MealList, ","){
	//		value, err := strconv.ParseInt(m, 10, 64)
	//		if err != nil{
	//			return map[string]interface{}{"code":400, "mesage":"转换失败"}, nil
	//		}
	//		for _, tcp := range entityProductList{
	//			if value == tcp.Id{
	//				//...
	//				return map[string]interface{}{"code":200, "mesage":"成功"}, nil
	//			}
	//		}
	//	}
	}

	return map[string]interface{}{"code":200, "message":"成功"}, nil
}

//func (order *Order)ConsultantConfirmation(oId int64)(map[string]interface{}, error){
//
//	var o Order
//	x.Id(oId).Get(&o)
//
//	var a Adviser
//	x.Id(o.NextSinglePersonId).Get(&a)
//
//	var dma DepartMentAudit
//
//	if o.Type == 1 || o.Type == 2 || o.Type == 11{
//		var ecList []*ExpressChange
//		x.Where("order_id = ?", o.Id).In("state", []int{0, 1}).Find(&ecList)
//		if len(ecList) > 0{
//			return map[string]interface{}{"code":400, "mesage":"还存在未审核快递变更"}, nil
//		}
//	}
//
//	if o.Type == 5{
//		x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 2).Get(&dma)
//		if dma.Id != 0{
//			var loa = GeneralAudit{
//				OrderId:   o.Id,
//				State:     1,
//				CreatedAt: time.Now(),
//				UpdatedAt: time.Now(),
//			}
//			x.Insert(&loa)
//
//			o.State = 3
//			x.Id(o.Id).Update(&o)
//
//			return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//		}
//
//		var trainingCampProductList []*Meal
//		x.Where("type = ?", 3).Find(&trainingCampProductList)
//		for _, m := range strings.Split(o.MealList, ","){
//			value, err := strconv.ParseInt(m, 10, 64)
//			if err != nil{
//				return map[string]interface{}{"code":400, "mesage":"转换失败"}, nil
//			}
//			for _, tcp := range trainingCampProductList{
//				if value == tcp.Id{
//					var ba = BusinessAudit{
//						OrderId:   o.Id,
//						State:     1,
//						CreatedAt: time.Now(),
//						UpdatedAt: time.Now(),
//					}
//					x.Insert(&ba)
//
//					return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//				}
//			}
//		}
//
//		o.State = 5
//		x.Id(o.Id).Update(&o)
//	}
//
//	if o.Type == 6 || o.Type == 8 || o.Type == 9 || o.Type == 10{
//		var loa = LargeOrderAudit{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&loa)
//
//		o.State = 2
//		x.Id(o.Id).Update(&o)
//
//		return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//	}
//
//	if o.Type == 7{
//		var firstOrder Order
//		x.Where("customer_id = ?", o.CustomerId).And("type = ?", 7).
//			Asc("created_at").
//			NotIn("order_state", []int{4, 22, 40, 91}).Desc("created_at").
//			Limit(1).Get(&firstOrder)
//		if firstOrder.TotalPrice > 100000 && firstOrder.TotalPrice <= 300000{
//			var da = DirectorAudit{
//				OrderId:   o.Id,
//				State:     1,
//				CreatedAt: time.Now(),
//				UpdatedAt: time.Now(),
//			}
//			x.Insert(&da)
//
//			o.State = 7
//			x.Id(o.Id).Update(&o)
//			return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//		}else {
//			var loa = LargeOrderAudit{
//				OrderId:   o.Id,
//				State:     1,
//				CreatedAt: time.Now(),
//				UpdatedAt: time.Now(),
//			}
//			x.Insert(&loa)
//
//			o.State = 2
//			x.Id(o.Id).Update(&o)
//
//			return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//		}
//	}
//
//	x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 1).Get(&dma)
//	if dma.Id != 0{
//		var loa = LargeOrderAudit{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&loa)
//
//		o.State = 2
//		x.Id(o.Id).Update(&o)
//
//		return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//	}
//
//	x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 2).Get(&dma)
//	if dma.Id != 0{
//		var loa = GeneralAudit{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&loa)
//
//		o.State = 3
//		x.Id(o.Id).Update(&o)
//
//		return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//	}
//
//	o.State = 5
//	x.Id(o.Id).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}

type Audit struct {
	Id int64 `xorm:"pk autoincr"`
	OrderId int64
	Reviewer int64
	ReviewerName string
	State int  // 1新提交  2审核通过  3审核不通过
	OrderStatus int
	CreatedAt time.Time
	UpdatedAt time.Time
}

/*
1新下单  2大单审单  3普通审单  4业务审单  5发仓库  7订单激活  9情感审单  10签收  -1主管审单  -2总监审单  -3取消订单
 */

func (order *Order)ConsultantConfirmationOrder(oId int64)(map[string]interface{}, error){

	w := new(utils.Worker)

	var o Order
	x.Id(oId).Get(&o)

	var a Adviser
	x.Id(o.NextSinglePersonId).Get(&a)

	var dma DepartMentAudit

	if o.Type == 1 || o.Type == 2 || o.Type == 11{
		var ecList []*ExpressChange
		x.Where("order_id = ?", o.Id).In("state", []int{1, 2}).Find(&ecList)
		if len(ecList) > 0{
			return map[string]interface{}{"code":400, "message":"还存在未审核快递变更"}, nil
		}
	}

	if o.Type == 5{
		x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 305815749639798785).Get(&dma)
		if dma.Id != 0{
			var loa = Audit{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				OrderStatus: 3,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&loa)

			o.State = 3
			x.Id(o.Id).Update(&o)

			return map[string]interface{}{"code":200, "message":"确认成功"}, nil
		}

		var trainingCampProductList []*Meal
		x.Where("type = ?", 3).Find(&trainingCampProductList)
		for _, m := range strings.Split(o.MealList, ","){
			value, err := strconv.ParseInt(m, 10, 64)
			if err != nil{
				return map[string]interface{}{"code":400, "message":"转换失败"}, nil
			}
			for _, tcp := range trainingCampProductList{
				if value == tcp.Id{
					var ba = Audit{
						Id:        w.GetId(),
						OrderId:   o.Id,
						State:     1,
						OrderStatus: 4,
						CreatedAt: time.Now(),
						UpdatedAt: time.Now(),
					}
					x.Insert(&ba)

					o.State = 4
					x.Id(o.Id).Update(&o)

					return map[string]interface{}{"code":200, "message":"确认成功"}, nil
				}
			}
		}

		o.State = 5
		x.Id(o.Id).Update(&o)

		return map[string]interface{}{"code":200, "message":"确认成功"}, nil
	}

	if o.Type == 6 || o.Type == 8 || o.Type == 9 || o.Type == 10{
		var loa = Audit{
			Id:        w.GetId(),
			OrderId:   o.Id,
			State:     1,
			OrderStatus: 2,
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		x.Insert(&loa)

		o.State = 2
		x.Id(o.Id).Update(&o)

		return map[string]interface{}{"code":200, "message":"确认成功"}, nil
	}

	if o.Type == 7{
		var orderList []*Order
		x.Where("customer_id = ?", o.CustomerId).
			And("type = ?", 7).
			NotIn("order_state", []int{22, 40, 91}).
			Desc("created_at").
			Find(&orderList)
		if len(orderList) < 2 && o.TotalPrice > 100000 && o.TotalPrice <= 200000{
			var sa = Audit{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				OrderStatus: -2,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&sa)

			o.State = -2
			x.Id(o.Id).Update(&o)
			return map[string]interface{}{"code":200, "message":"确认成功"}, nil
		}else {
			var loa = Audit{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				OrderStatus: 2,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&loa)

			o.State = 2
			x.Id(o.Id).Update(&o)

			return map[string]interface{}{"code":200, "message":"确认成功"}, nil
		}
	}

	x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 305815749639798784).Get(&dma)
	if dma.Id != 0{
		var loa = Audit{
			Id:        w.GetId(),
			OrderId:   o.Id,
			State:     1,
			OrderStatus: 2,
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		x.Insert(&loa)

		o.State = 2
		x.Id(o.Id).Update(&o)

		return map[string]interface{}{"code":200, "message":"确认成功"}, nil
	}

	x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 305815749639798785).Get(&dma)
	if dma.Id != 0{
		var loa = Audit{
			Id:        w.GetId(),
			OrderId:   o.Id,
			State:     1,
			OrderStatus: 3,
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		x.Insert(&loa)

		o.State = 3
		x.Id(o.Id).Update(&o)

		return map[string]interface{}{"code":200, "message":"确认成功"}, nil
	}

	o.State = 5
	x.Id(o.Id).Update(&o)

	return map[string]interface{}{"code":200, "message":"确认成功"}, nil
}

func (order *Order)ToExamine(aId int64, auditId int64)(map[string]interface{}, error){

	w := new(utils.Worker)

	var audit Audit
	x.Id(auditId).Get(&audit)

	var o Order
	x.Id(audit.OrderId).Get(&o)

	var adviser Adviser
	x.Id(aId).Get(&adviser)

	if audit.OrderStatus == 2{
		if o.Type == 3 || o.Type == 6 || o.Type == 7 || o.Type == 8 || o.Type == 9 || o.Type == 10{
			var ga = Audit{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				OrderStatus: 3,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&ga)

			audit.Reviewer = aId
			audit.ReviewerName = adviser.Name
			audit.State = 2
			x.Id(audit.Id).Update(&audit)

			o.State = 3
			x.Id(o.Id).Update(&o)

			return map[string]interface{}{"code":200, "message":"审单通过"}, nil
		}

		var a Adviser
		x.Id(o.NextSinglePersonId).Get(&a)

		var dma DepartMentAudit
		x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 305815749639798785).Get(&dma)
		if dma.Id != 0 {
			var ga = Audit{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				OrderStatus: 3,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&ga)

			audit.Reviewer = aId
			audit.ReviewerName = adviser.Name
			audit.State = 2
			x.Id(audit.Id).Update(&audit)

			o.State = 3
			x.Id(o.Id).Update(&o)

			return map[string]interface{}{"code":200, "message":"审单通过"}, nil
		}

		audit.Reviewer = aId
		audit.ReviewerName = adviser.Name
		audit.State = 2
		x.Id(audit.Id).Update(&audit)

		o.State = 5
		x.Id(o.Id).Update(&o)

		return map[string]interface{}{"code":200, "message":"审单通过"}, nil
	}else if audit.OrderStatus == 3{
		if o.Type == 6 || o.Type == 8 ||o.Type == 10{
			var ba = Audit{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				OrderStatus: 4,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&ba)

			audit.Reviewer = aId
			audit.ReviewerName = adviser.Name
			audit.State = 2
			x.Id(audit.Id).Update(&audit)

			o.State = 4
			x.Id(audit.OrderId).Update(&o)

			return map[string]interface{}{"code":200, "message":"审单通过"}, nil
		}

		if o.Type == 7{
			var oa = OrderActivation{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&oa)

			audit.Reviewer = aId
			audit.ReviewerName = adviser.Name
			audit.State = 2
			x.Id(audit.Id).Update(&audit)

			o.State = 7
			x.Id(audit.OrderId).Update(&o)

			return map[string]interface{}{"code":200, "message":"审单通过"}, nil
		}

		if o.Type == 9{
			var oList []*Order
			x.Where("customer_id = ?", o.CustomerId).And("type = ?", 9).Find(&oList)
			if len(oList) < 2 {
				var eca = Audit{
					Id:        w.GetId(),
					OrderId:   o.Id,
					State:     1,
					OrderStatus: 9,
					CreatedAt: time.Now(),
					UpdatedAt: time.Now(),
				}
				x.Insert(&eca)

				audit.Reviewer = aId
				audit.ReviewerName = adviser.Name
				audit.State = 2
				x.Id(audit.Id).Update(&audit)

				o.State = 9
				x.Id(audit.OrderId).Update(&o)

				return map[string]interface{}{"code":200, "message":"审单通过"}, nil
			}else {
				audit.Reviewer = aId
				audit.ReviewerName = adviser.Name
				audit.State = 2
				x.Id(audit.Id).Update(&audit)

				o.State = 5
				x.Id(audit.OrderId).Update(&o)

				return map[string]interface{}{"code":200, "message":"审单通过"}, nil
			}
		}

		audit.Reviewer = aId
		audit.ReviewerName = adviser.Name
		audit.State = 2
		x.Id(audit.Id).Update(&audit)

		o.State = 5
		x.Id(audit.OrderId).Update(&o)

		return map[string]interface{}{"code":200, "message":"审单通过"}, nil
	}else if audit.OrderStatus == 4{
		if o.Type == 8 || o.Type == 10{
			var oa = OrderActivation{
				Id:        w.GetId(),
				OrderId:   o.Id,
				State:     1,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			x.Insert(&oa)

			audit.Reviewer = aId
			audit.ReviewerName = adviser.Name
			audit.State = 2
			x.Id(audit.Id).Update(&audit)

			o.State = 7
			x.Id(audit.OrderId).Update(&o)

			return map[string]interface{}{"code":200, "message":"审单通过"}, nil
		}

		var entityProductList []*Meal
		x.Where("entity = ?", true).Find(&entityProductList)
		for _, m := range strings.Split(o.MealList, ","){
			value, err := strconv.ParseInt(m, 10, 64)
			if err != nil{
				return map[string]interface{}{"code":400, "message":"转换失败"}, nil
			}
			for _, tcp := range entityProductList{
				if value == tcp.Id{

					audit.Reviewer = aId
					audit.ReviewerName = adviser.Name
					audit.State = 2
					x.Id(audit.Id).Update(&audit)

					o.State = 5
					x.Id(audit.OrderId).Update(&o)

					return map[string]interface{}{"code":200, "message":"审单通过"}, nil
				}
			}
		}

		audit.Reviewer = aId
		audit.ReviewerName = adviser.Name
		audit.State = 2
		x.Id(audit.Id).Update(&audit)

		o.State = 10
		x.Id(audit.OrderId).Update(&o)

		return map[string]interface{}{"code":200, "message":"审单通过"}, nil
	}else if audit.OrderStatus == 9{
		audit.Reviewer = aId
		audit.ReviewerName = adviser.Name
		audit.State = 2
		x.Id(audit.Id).Update(&audit)

		o.State = 5
		x.Id(audit.OrderId).Update(&o)

		return map[string]interface{}{"code":200, "message":"审单通过"}, nil
	}else if audit.OrderStatus == -1{

		audit.Reviewer = aId
		audit.ReviewerName = adviser.Name
		audit.State = 2
		x.Id(audit.Id).Update(&audit)

		o.State = 1
		x.Id(audit.OrderId).Update(&o)

		return map[string]interface{}{"code":200, "message":"审单通过"}, nil

	}else if audit.OrderStatus == -2{

		var loa = Audit{
			Id:        w.GetId(),
			OrderId:   o.Id,
			State:     1,
			OrderStatus: 2,
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		}
		x.Insert(&loa)

		audit.Reviewer = aId
		audit.ReviewerName = adviser.Name
		audit.State = 2
		x.Id(audit.Id).Update(&audit)

		o.State = 2
		x.Id(audit.OrderId).Update(&o)

		return map[string]interface{}{"code":200, "message":"审单通过"}, nil
	}

	return map[string]interface{}{"code":200, "message":"审单通过"}, nil
}

func (order *Order)QueryAudit(adviserId int64)([]*Audit, error){

	var adviser Adviser
	x.Id(adviserId).Get(&adviser)

	var auditList []*Audit

	if adviser.RoleId == 305814115698343937{
		x.Where("order_status = ?", 2).Find(&auditList)
	}else if adviser.RoleId == 305814115698343938{
		x.Where("order_status = ?", 3).Find(&auditList)
	}else if adviser.RoleId == 305814115698343939{
		x.Where("order_status = ?", 4).Find(&auditList)
	}else if adviser.RoleId == 305814115698343940{
		x.Where("order_status = ?", 9).Find(&auditList)
	}else if adviser.RoleId == 305814115698343941{
		fmt.Println("adviser.DepartMentId：", adviser.DepartMentId)
		//x.Join("INNER", "order", "order.id = audit.order_id").
		//	Join("INNER", "adviser", "adviser.id = order.next_single_person_id").
		//	Where("adviser.depart_ment_id = ?", adviser.DepartMentId).
		//	And("audit.order_status = ?", -1).Find(&auditList)

		x.SQL("select a.* from audit a inner join `order` o on o.id = a.order_id inner join adviser s on s.id = o.next_single_person_id where s.depart_ment_id = ? and a.order_status = -1", adviser.DepartMentId).Find(&auditList)
		fmt.Println("auditList：", auditList)
	}else if adviser.RoleId == 305814115698343942{
		x.Join("INNER", "`order`", "`order`.id = audit.order_id").
			Join("INNER", "adviser", "adviser.id = `order`.next_single_person_id").
			Where("adviser.depart_ment_id = ?", adviser.DepartMentId).
			And("audit.order_status = ?", -2).Find(&auditList)
	}

	return auditList, nil
}

func (order *Order)ReviewReturn(adviserId int64, auditId int64)(map[string]interface{}, error){

	var audit Audit
	x.Id(auditId).Get(&audit)

	var o Order
	x.Id(audit.OrderId).Get(&o)

	var adviser Adviser
	x.Id(adviserId).Get(&adviser)

	audit.Reviewer = adviserId
	audit.ReviewerName = adviser.Name
	audit.State = 3
	x.Id(audit.Id).Update(&audit)

	if audit.OrderStatus != -1 {
		o.State = 1
	}else {
		o.State = -3
	}

	x.Id(audit.OrderId).Update(&o)

	return map[string]interface{}{"code": 200, "message": "退回成功"}, nil
}

//func LargeConfirmation(aId int64, loaId int64)(map[string]interface{}, error){
//
//	var loa LargeOrderAudit
//	x.Id(loaId).Get(&loa)
//
//	var o Order
//	x.Id(loa.OrderId).Get(&o)
//
//	if o.Type == 3 || o.Type == 6 || o.Type == 7 || o.Type == 8 || o.Type == 9 || o.Type == 10{
//		var ga = GeneralAudit{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&ga)
//
//		loa.Reviewer = aId
//		loa.State = 2
//		loa.Result = 2
//		x.Id(loa.Id).Update(&loa)
//
//		o.State = 3
//		x.Id(o.Id).Update(&o)
//
//		return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//	}
//
//	var a Adviser
//	x.Id(o.NextSinglePersonId).Get(&a)
//
//	var dma DepartMentAudit
//	x.Where("depart_ment_id = ?", a.DepartMentId).And("audit_type_id = ?", 2).Get(&dma)
//	if dma.Id != 0 {
//		var ga = GeneralAudit{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&ga)
//
//		loa.Reviewer = aId
//		loa.State = 2
//		loa.Result = 2
//		x.Id(loa.Id).Update(&loa)
//
//		o.State = 3
//		x.Id(o.Id).Update(&o)
//
//		return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//	}
//
//	o.State = 5
//	x.Id(o.Id).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func GeneralConfirmation(aId int64, gaId int64)(map[string]interface{}, error){
//
//	var ga GeneralAudit
//	x.Id(gaId).Get(&ga)
//
//	var o Order
//	x.Id(ga.OrderId).Get(&o)
//	if o.Type == 6 || o.Type == 10{
//		var ba = BusinessAudit{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&ba)
//
//		o.State = 5
//	}
//
//	if o.Type == 7{
//		var oa = OrderActivation{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&oa)
//
//		o.State = 6
//	}
//
//	if o.Type == 9{
//		var oList []*Order
//		x.Where("customer_id = ?", o.CustomerId).And("type = ?", 9).Find(&oList)
//		if len(oList) > 1 {
//			var eca = EmotionalCounselingAudit{
//				OrderId:   o.Id,
//				State:     1,
//				CreatedAt: time.Now(),
//				UpdatedAt: time.Now(),
//			}
//			x.Insert(&eca)
//		}else {
//			ga.Reviewer = aId
//			ga.State = 1
//			ga.Result = 1
//			x.Id(ga.Id).Update(&ga)
//
//			o.State = 5
//			x.Id(ga.OrderId).Update(&o)
//
//			return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//		}
//	}
//
//	ga.Reviewer = aId
//	ga.State = 2
//	ga.Result = 2
//	x.Id(ga.Id).Update(&ga)
//
//	x.Id(ga.OrderId).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func BusinessConfirmation(aId int64, baId int64)(map[string]interface{}, error){
//
//	var ba GeneralAudit
//	x.Id(baId).Get(&ba)
//
//	var o Order
//	x.Id(ba.OrderId).Get(&o)
//
//	if o.Type == 8{
//		var oa = OrderActivation{
//			OrderId:   o.Id,
//			State:     1,
//			CreatedAt: time.Now(),
//			UpdatedAt: time.Now(),
//		}
//		x.Insert(&oa)
//	}
//
//	ba.Reviewer = aId
//	ba.State = 2
//	ba.Result = 2
//	x.Id(ba.Id).Update(&ba)
//
//	var entityProductList []*Meal
//	x.Where("entity = ?", true).Find(&entityProductList)
//	for _, m := range strings.Split(o.MealList, ","){
//		value, err := strconv.ParseInt(m, 10, 64)
//		if err != nil{
//			return map[string]interface{}{"code":400, "mesage":"转换失败"}, nil
//		}
//		for _, tcp := range entityProductList{
//			if value == tcp.Id{
//
//				o.State = 5
//				x.Id(ba.OrderId).Update(&o)
//
//				return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//			}
//		}
//	}
//
//	o.State = 10
//	x.Id(ba.OrderId).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func SupervisorConfirmation(aId int64, saId int64)(map[string]interface{}, error){
//
//	var sa DirectorAudit
//	x.Id(saId).Get(&sa)
//
//	var o Order
//
//	sa.Reviewer = aId
//	sa.State = 2
//	sa.Result = 2
//	x.Id(sa.Id).Update(&sa)
//
//	o.State = 1
//	x.Id(sa.OrderId).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func DirectorConfirmation(aId int64, daId int64)(map[string]interface{}, error){
//
//	var da DirectorAudit
//	x.Id(daId).Get(&da)
//
//	var o Order
//	x.Id(da.OrderId).Get(&o)
//
//	var loa = LargeOrderAudit{
//		OrderId:   o.Id,
//		State:     1,
//		CreatedAt: time.Now(),
//		UpdatedAt: time.Now(),
//	}
//	x.Insert(&loa)
//
//	da.Reviewer = aId
//	da.State = 2
//	da.Result = 2
//	x.Id(da.Id).Update(&da)
//
//	o.State = 2
//	x.Id(da.OrderId).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func EmotionalCounselingConfirmation(aId int64, ecaId int64)(map[string]interface{}, error){
//
//	var eca EmotionalCounselingAudit
//	x.Id(ecaId).Get(&eca)
//
//	var o Order
//
//	eca.Reviewer = aId
//	eca.State = 2
//	eca.Result = 2
//	x.Id(eca.Id).Update(&eca)
//
//	o.State = 5
//	x.Id(eca.OrderId).Update(&o)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func ReturnConfirmation(i interface{}, aId int64)(map[string]interface{}, error){
//
//	switch t := i.(type) {
//	case LargeOrderAudit:
//		t.State = 2
//		t.Reviewer = aId
//		t.Result = 2
//		x.Id(t.Id).Update(&t)
//
//		var o Order
//		o.State = 1
//		x.Id(t.OrderId).Update(&o)
//
//	case GeneralAudit:
//		t.State = 2
//		t.Reviewer = aId
//		t.Result = 2
//		x.Id(t.Id).Update(&t)
//
//		var o Order
//		o.State = 1
//		x.Id(t.OrderId).Update(&o)
//
//	case BusinessAudit:
//		t.State = 2
//		t.Reviewer = aId
//		t.Result = 2
//		x.Id(t.Id).Update(&t)
//
//		var o Order
//		o.State = 1
//		x.Id(t.OrderId).Update(&o)
//
//	case DirectorAudit:
//		t.State = 2
//		t.Reviewer = aId
//		t.Result = 2
//		x.Id(t.Id).Update(&t)
//
//		var o Order
//		o.State = 1
//		x.Id(t.OrderId).Update(&o)
//
//	case EmotionalCounselingAudit:
//		t.State = 2
//		t.Reviewer = aId
//		t.Result = 2
//		x.Id(t.Id).Update(&t)
//
//		var o Order
//		o.State = 1
//		x.Id(t.OrderId).Update(&o)
//	default:
//		return map[string]interface{}{"code":400, "mesage":"类型匹配失败"}, nil
//	}
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}
//
//func Distribution(eaId int64, aId int64)(map[string]interface{}, error){
//
//	var ea EmotionalAnalysis
//	ea.Analyst = aId
//	x.Id(eaId).Update(&ea)
//
//	return map[string]interface{}{"code":200, "mesage":"成功"}, nil
//}















